<template>
  <el-dialog
    :title="!dataForm.clubId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="榜单名称" prop="clubName">
      <el-input v-model="dataForm.clubName" placeholder="榜单名称"></el-input>
    </el-form-item>
    <el-form-item label="榜单分类id" prop="catId">
      <category-cascader></category-cascader>
    </el-form-item>
    <el-form-item label="榜单分类名称" prop="catName">
      <el-input v-model="dataForm.catName" placeholder="所属榜单分类名称"></el-input>
    </el-form-item>
    <el-form-item label="榜单描述" prop="descript">
      <el-input v-model="dataForm.descript" placeholder="榜单描述"></el-input>
    </el-form-item>
    <el-form-item label="榜单成员数量" prop="memberCount">
      <el-input v-model.number="dataForm.memberCount" placeholder="榜单成员数量"></el-input>
    </el-form-item>
    <el-form-item label="榜单logo地址" prop="logo">
      <!-- <el-input v-model="dataForm.logo" placeholder="榜单logo地址"></el-input> -->
      <single-upload v-model="dataForm.logo"></single-upload>
    </el-form-item>
    <el-form-item label="检索首字母" prop="firstLetter">
      <el-input v-model="dataForm.firstLetter" placeholder="检索首字母"></el-input>
    </el-form-item>
    <el-form-item label="顺序" prop="sort">
      <el-input v-model.number="dataForm.sort" placeholder="顺序"></el-input>
    </el-form-item>
    
    <el-form-item label="创建人id" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建人id"></el-input>
    </el-form-item>
    <el-form-item label="创建人名称" prop="createUserName">
      <el-input v-model="dataForm.createUserName" placeholder="创建人名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
  import CategoryCascader from "../common/category-cascader";
  export default {
    components: { SingleUpload, CategoryCascader},
    data () {
      return {
        catPathSub: null,
        visible: false,
        dataForm: {
          clubId: 0,
          clubName: '',
          catId: '',
          catName: '',
          descript: '',
          memberCount: null,
          logo: '',
          firstLetter: '',
          sort: null,
          createUserId: '',
          createUserName: ''
        },
        dataRule: {
          clubName: [
            { required: true, message: '榜单名称不能为空', trigger: 'blur' }
          ],
          catId: [
            { required: true, message: '所属榜单分类id不能为空', trigger: 'blur' }
          ],
          catName: [
            { required: true, message: '所属榜单分类名称不能为空', trigger: 'blur' }
          ],
          descript: [
            { required: true, message: '榜单描述不能为空', trigger: 'blur' }
          ],
          memberCount: [
            {
            validator: (rule, value, callback) => {
              if (value == null) {
                callback(new Error("榜单成员数量必须填写"));
              } else if (!Number.isInteger(value) || value<0) {
                callback(new Error("榜单成员数量必须是一个大于等于0的整数"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
          ],
          logo: [
            { required: true, message: '榜单logo地址不能为空', trigger: 'blur' }
          ],
          firstLetter: [
            {
            validator: (rule, value, callback) => {
              if (value == "") {
                callback(new Error("首字母必须填写"));
              } else if (!/^[a-zA-Z]$/.test(value)) {
                callback(new Error("首字母必须a-z或者A-Z之间"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
          ],
          sort: [
            {
            validator: (rule, value, callback) => {
              if (value == null) {
                callback(new Error("排序字段必须填写"));
              } else if(!Number.isInteger(value) || value<0){
                callback(new Error('排序必须是一个大于等于0的整数'));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
          ],
          
          createUserId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          createUserName: [
            { required: true, message: '创建人名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.clubId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.clubId) {
            this.$http({
              url: this.$http.adornUrl(`/rank/club/info/${this.dataForm.clubId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.clubName = data.club.clubName
                this.dataForm.catId = data.club.catId
                this.dataForm.catName = data.club.catName
                this.dataForm.descript = data.club.descript
                this.dataForm.memberCount = data.club.memberCount
                this.dataForm.logo = data.club.logo
                this.dataForm.firstLetter = data.club.firstLetter
                this.dataForm.sort = data.club.sort
                this.dataForm.createUserId = data.club.createUserId
                this.dataForm.createUserName = data.club.createUserName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rank/club/${!this.dataForm.clubId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'clubId': this.dataForm.clubId || undefined,
                'clubName': this.dataForm.clubName,
                'catId': this.dataForm.catId,
                'catName': this.dataForm.catName,
                'descript': this.dataForm.descript,
                'memberCount': this.dataForm.memberCount,
                'logo': this.dataForm.logo,
                'firstLetter': this.dataForm.firstLetter,
                'sort': this.dataForm.sort,
                'createUserId': this.dataForm.createUserId,
                'createUserName': this.dataForm.createUserName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
      this.catPathSub = PubSub.subscribe("catPath", (msg, val) => {
        this.dataForm.catId = val[val.length - 1];
      });
    },
  }
</script>
